package com.baizhi.controller;


import com.baizhi.entity.Attr;
import com.baizhi.service.IAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author huiJie
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/attr")
public class AttrController {
    @Autowired
    private IAttrService attrService;
    @GetMapping("{id}")
    public R showAttr(@PathVariable("id")Integer id){
        List<Attr> attrs = attrService.list(new QueryWrapper<Attr>().eq("category_id", id).eq("attr_type",0));
        return R.ok(attrs);
    }
    @GetMapping("1/{id}")
    public R showAttr1(@PathVariable("id")Integer id){
        List<Attr> attrs = attrService.list(new QueryWrapper<Attr>().eq("category_id", id).eq("attr_type",1));
        return R.ok(attrs);
    }
    @PutMapping
    public R updateEnable(@RequestBody Attr attr){
        if (attr.getEnable()==0){
            attr.setEnable(1);
        }else{
            attr.setEnable(0);
        }
        QueryWrapper<Attr> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attr.getAttrId());
        attrService.update(attr,wrapper);
        return R.ok(null);
    }
}

